import api from '../../api/index'
import store from '../../store/index'
const app = getApp()
import IMService from '../../static/lib/imservice.js';
import userInfo from '../../api/userInfo';

Component({

  properties: {
    active: Boolean,
    title: String,
    color: {
      type: String,
      value: '#808080'
    },
    getUserInfo: Boolean
  },

  methods: {
    tapAction: function(e) {
      if (this.data.active) {
        this.triggerEvent('Action')
      }
    },
    getUserInfo: function(e) {
      if (!e.detail.userInfo) { return; }
      console.log(e.detail)
      api.user.saveUserInfo(e.detail.userInfo)
        .then(() => {
          store.refreshUser()
          this.triggerEvent('Action')
        })
    },
    getUserProfile(e) {
      // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
      // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
      wx.getUserProfile({
        desc: '用于完善用户资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          console.log('=====================', res.userInfo)
          api.user.saveUserInfo(res.userInfo).then(()=>{
            store.refreshUser()
          })
          app.globalData.imService = new IMService(wx.im);
            var currentUser = {
              uuid: res.userInfo._openid,
              avatar: res.userInfo.avatarUrl,
              name: res.userInfo.nickName
             }
            app.globalData.imService.connectIM(currentUser);
        }
      })
    },
    getPhoneNumber (e) {
      console.log(e.detail.errMsg)
      console.log(e.detail.iv)
      console.log(e.detail.encryptedData)
    }
  }

})
